﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iLangl.Model;
using iLangl.BusinessModel.DAL;

namespace iLangl.BusinessModel.Managers
{
	public class LanguageManager
	{
		#region Singelton Pattern
		private LanguageManager()
		{
		}

		private static LanguageManager instance;
		private static object mutex = new object();
		/// <summary>
		/// The instance of the LanguageManager.
		/// </summary>
		public static LanguageManager Instance
		{
			get
			{
				if (instance == null)
				{
					lock (mutex)
					{
						if (instance == null)
						{
							instance = new LanguageManager();
						}
					}
				}
				return instance;
			}
		}
		#endregion

		public void RegisterLanguages(IList<Language> languages)
		{
			DALFacade.Instance.LanguageDAL.RegisterLanguages(languages, null);
		}

		public Language FindLanguageByName(string name)
		{
			foreach (Language lang in GetLanguages())
			{
				if(String.Compare(lang.LanguageName, name, true) == 0)
				{
					return lang;
				}
			}

			return DefaultValueManager.language;
		}

		public IList<Language> GetLanguages()
		{
			return DALFacade.Instance.LanguageDAL.GetLanguages();
		}

		public void RegisterPluginLanguages(IList<PluginLanguage> languages)
		{
			DALFacade.Instance.LanguageDAL.RegisterPluginLanguages(languages, null);
		}
		
		public Language GetLanguage(String pluginId, String code)
		{
			return DALFacade.Instance.LanguageDAL.GetLanguage(pluginId, code);
		}

		public Language GetPluginLanguage(string pluginId, string languagecode)
		{
			return DALFacade.Instance.LanguageDAL.GetPluginLanguage(pluginId, languagecode);
		}
	}
}
